

package com.jfinal.aceadmin.model;

import com.jfinal.ext.plugin.tablebind.TableBind;

import com.jfinal.plugin.activerecord.Db;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <a href="UserMessage.java.html"><b><i>View Source</i></b></a>
 *
 * @author Jason
 *
 */
@TableBind(tableName = "UserMessage", pkName = "baseId")
public class UserMessage extends BaseModel<UserMessage> {
	private static final long serialVersionUID = 1L;
	public static UserMessage dao = new UserMessage();

	public long getBaseId() {
		return this.get("baseId");
	}

	public void setBaseId(long baseId) {
		this.set("baseId", baseId);
	}

	public int getUserId() {
		return this.get("userId");
	}

	public void setUserId(int userId) {
		this.set("userId", userId);
	}

	public int getSenderId() {
		return this.get("senderId");
	}

	public void setSenderId(int senderId) {
		this.set("senderId", senderId);
	}

	public String getSenderName() {
		return this.get("senderName");
	}

	public void setSenderName(String senderName) {
		this.set("senderName", senderName);
	}

	public String getContent() {
		return this.get("content");
	}

	public void setContent(String content) {
		this.set("content", content);
	}

	public Date getCreatedDate() {
		return this.get("createdDate");
	}

	public void setCreatedDate(Date createdDate) {
		this.set("createdDate", createdDate);
	}

	public boolean getReaded() {
		return this.get("readed");
	}

	public void setReaded(boolean readed) {
		this.set("readed", readed);
	}

	public List<UserMessage> findByUserId(int userId) {
		List<UserMessage> result = new ArrayList<UserMessage>();
		StringBuilder sBuilder = new StringBuilder(
				"SELECT * FROM UserMessage WHERE ");
		sBuilder.append("userId = ?");
		sBuilder.append(" ORDER BY ");
		sBuilder.append("createdDate DESC");

		result = UserMessage.dao.find(sBuilder.toString(), userId);

		return result;
	}

	public int countByUserId(int userId) {
		int result = 0;
		StringBuilder sBuilder = new StringBuilder(
				"SELECT COUNT(*) FROM UserMessage WHERE ");
		sBuilder.append("userId = ?");
		result = Db.queryLong(sBuilder.toString(), userId).intValue();

		return result;
	}

	public List<UserMessage> findByUserIdAndRead(int userId, boolean readed) {
		List<UserMessage> result = new ArrayList<UserMessage>();
		StringBuilder sBuilder = new StringBuilder(
				"SELECT * FROM UserMessage WHERE ");
		sBuilder.append("userId = ?");
		sBuilder.append(" AND ");
		sBuilder.append("readed = ?");
		sBuilder.append(" ORDER BY ");
		sBuilder.append("createdDate DESC");

		result = UserMessage.dao.find(sBuilder.toString(), userId, readed);

		return result;
	}

	public int countByUserIdAndRead(int userId, boolean readed) {
		int result = 0;
		StringBuilder sBuilder = new StringBuilder(
				"SELECT COUNT(*) FROM UserMessage WHERE ");
		sBuilder.append("userId = ?");
		sBuilder.append(" AND ");
		sBuilder.append("readed = ?");
		result = Db.queryLong(sBuilder.toString(), userId, readed).intValue();

		return result;
	}
}